package cn.com.dragonsoftbravo.utils.azure.factory;

import java.net.URI;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import cn.com.dragonsoftbravo.utils.SystemConfigHolder;
import cn.com.dragonsoftbravo.utils.azure.AzureConstants;

import com.microsoft.azure.storage.CloudStorageAccount;
import com.microsoft.azure.storage.StorageCredentials;
import com.microsoft.azure.storage.StorageCredentialsAccountAndKey;
import com.microsoft.azure.storage.blob.CloudBlobClient;
import com.microsoft.azure.storage.table.CloudTableClient;

public class BlobClientFactory {

	private static CloudBlobClient blobClient;

	private static CloudTableClient tableClient;

	private static String accountName;

	private static String accountKey;

	private static String blobEndpointURI;

	private static String queueEndpointURI;

	private static String tableEndpointURI;

	static {
		accountName = SystemConfigHolder.instance().getValue(AzureConstants.ACCOUNT_NAME);
		accountKey = SystemConfigHolder.instance().getValue(AzureConstants.ACCOUNT_KEY);
		blobEndpointURI = SystemConfigHolder.instance().getValue(AzureConstants.BLOB_ENDPOINT_URI);
		queueEndpointURI = SystemConfigHolder.instance().getValue(AzureConstants.QUEUE_ENDPOINT_URI);
		tableEndpointURI = SystemConfigHolder.instance().getValue(AzureConstants.TABLE_ENDPOINT_URI);
	}

	public static void init() {

		StorageCredentials credentials = new StorageCredentialsAccountAndKey(accountName, accountKey);
		URI blobEndpoint = URI.create(blobEndpointURI);
		URI queueEndpoint = URI.create(queueEndpointURI);
		URI tableEndpoint = URI.create(tableEndpointURI);
		CloudStorageAccount account = new CloudStorageAccount(credentials, blobEndpoint, queueEndpoint,
				tableEndpoint);
		initConnection();
		blobClient = account.createCloudBlobClient();
		tableClient = account.createCloudTableClient();
	}

	public static void initConnection() {
		try {
			TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

				public X509Certificate[] getAcceptedIssuers() {
					return null;
				}

				public void checkClientTrusted(X509Certificate[] certs, String authType) {
				}

				public void checkServerTrusted(X509Certificate[] certs, String authType) {
				}

			} };

			SSLContext sc = SSLContext.getInstance("TLS");
			sc.init(null, trustAllCerts, new SecureRandom());

			HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (KeyManagementException e) {
			e.printStackTrace();
		}
	}

	public static CloudBlobClient getCloudBlobClient() {
		if (null == blobClient) {
			init();
		}
		return blobClient;
	}

	public static CloudTableClient getTableClient() {
		if (null == tableClient) {
			init();
		}
		return tableClient;
	}
}
